package com.kaoyaya.delivery.adapter;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.kaoyaya.delivery.activity.R;
import com.kaoyaya.delivery.data.entity.Combo;

/**
 * 套餐Adapter
 * @date 2013-5-26
 * @author Goven
 * @email gxl3999@gmail.com
 */
public class ComboAdapter extends BaseExpandableListAdapter {

	private Context context;
	private List<Combo> combos;
	private LayoutInflater inflater;
	private View.OnClickListener clickListener;

	public ComboAdapter(Context context, View.OnClickListener clickListener, List<Combo> combos) {
		this.context = context;
		this.combos = combos;
		inflater = LayoutInflater.from(context);
		this.clickListener = clickListener;
	}
	
	public void addCombo(List<Combo> combos) {
		this.combos.addAll(combos);
	}

	@Override
	public Object getChild(int groupPosition, int childPosition) {
		return combos.get(groupPosition).getProducts().get(childPosition);
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		return childPosition;
	}

	@Override
	public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
		ViewHolderChild child;
		if (convertView == null) {
			child = new ViewHolderChild();
			convertView = inflater.inflate(R.layout.item_combo_child, null);
			child.tvName = (TextView) convertView.findViewById(R.id.tvName);
			child.tvArea = (TextView) convertView.findViewById(R.id.tvArea);
			child.tvSubject = (TextView) convertView.findViewById(R.id.tvSubject);
			child.tvPrice = (TextView) convertView.findViewById(R.id.tvPrice);
			convertView.setTag(child);
		} else {
			child = (ViewHolderChild) convertView.getTag();
		}
		child.tvName.setText(combos.get(groupPosition).getProducts().get(childPosition).getName());
		child.tvArea.setText(combos.get(groupPosition).getProducts().get(childPosition).getArea());
		child.tvSubject.setText(combos.get(groupPosition).getProducts().get(childPosition).getSubjectName());
		child.tvPrice.setText("￥" + String.valueOf(combos.get(groupPosition).getProducts().get(childPosition).getPrice()));
		return convertView;
	}

	@Override
	public int getChildrenCount(int groupPosition) {
		return combos.get(groupPosition).getProducts().size();
	}

	@Override
	public Object getGroup(int groupPosition) {
		return combos.get(groupPosition);
	}

	@Override
	public int getGroupCount() {
		return combos.size();
	}

	@Override
	public long getGroupId(int groupPosition) {
		return groupPosition;
	}

	@Override
	public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
		ViewHolderGroup groupHolder;
		if (convertView == null) {
			groupHolder = new ViewHolderGroup();
			convertView = inflater.inflate(R.layout.item_combo_group, null);
			groupHolder.ivExpand = (ImageView) convertView.findViewById(R.id.ivExpand);
			groupHolder.tvName = (TextView) convertView.findViewById(R.id.tvName);
			groupHolder.tvPrice = (TextView) convertView.findViewById(R.id.tvPrice);
			groupHolder.btnBuy = (Button) convertView.findViewById(R.id.btnBuy);
			groupHolder.btnBuy.setTag(combos.get(groupPosition));
			groupHolder.btnBuy.setOnClickListener(clickListener);
			convertView.setTag(groupHolder);
		} else {
			groupHolder = (ViewHolderGroup) convertView.getTag();
		}
		if (isExpanded) {
			groupHolder.ivExpand.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.ic_up));
		} else {
			groupHolder.ivExpand.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.ic_down));
		}
		groupHolder.tvName.setText(combos.get(groupPosition).getName());
		groupHolder.tvPrice.setText("￥" + String.valueOf(combos.get(groupPosition).getPrice()));
		return convertView;
	}

	@Override
	public boolean hasStableIds() {
		return false;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		return false;
	}

	private class ViewHolderGroup {
		Button btnBuy;
		TextView tvName;
		TextView tvPrice;
		ImageView ivExpand;
	}

	private class ViewHolderChild {
		TextView tvName;
		TextView tvArea;
		TextView tvSubject;
		TextView tvPrice;
	}
	
}
